/*  
 *  Copyright 2009 Anil Kumar

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 */

package htmlutil;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.tools.ant.BuildException;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;

public class TocfromXml {
	
	private String xmlfile;
	private String outputfile;
	private String msg;
	
	
	//public static void main(String[] args){
	public void execute() throws BuildException  {
		
		
		
		SAXBuilder builder = new SAXBuilder();
		
		Map<String,String> indexMap = new HashMap();
	     
	    // command line should offer URIs or file names
	    try {
	    	Document doc = builder.build(xmlfile);
	      // If there are no well-formedness errors, 
	      // then no exception is thrown
	      Element root = doc.getRootElement();
	      indexMap = getIndexMap(root, 0);
	      
	      String url="";
	      Iterator itr = indexMap.keySet().iterator();
		    while(itr.hasNext()){
		    	  Object key =  itr.next();
		    	try{  
		    	  url+="<li> <a href='"+ 
		    	  indexMap.get(key).substring(indexMap.get(key).indexOf("\\")+1) +
		    	  	"' target='maincontent' > "+ key + "</a> </li> \n";    	  
		    	
		    	}catch(NullPointerException e){} 
		    }
	      //print the html file
		    FileOutputStream out; // declare a file output object
            PrintStream p; // declare a print stream object

            try
            {
                    // Create a new file output stream
                    // connected to "myfile.txt"
                    out = new FileOutputStream(outputfile);

                    // Connect print stream to the output stream
                    p = new PrintStream( out );
	
                    p.println (url);
                    p.close();
            }
            catch (Exception e)
            {
                    System.err.println ("Error writing to file");
            }
		    
		    
	      
	      
	    }
	    // indicates a well-formedness error
	   catch (JDOMException e) { 
	      //System.out.println(args[0] + " is not well-formed.");
	      System.out.println(e.getMessage());
	    }  
	    catch (IOException e) { 
	      //System.out.println("Could not check " + args[0]);
	      System.out.println(" because " + e.getMessage());
	    }  
		
	    
			
		
		
	}
	
	public  Map<String,String> getIndexMap(Element current, int depth) {
		
//		main list contains all top level elements
	    Map<String,String> tocList = new HashMap();
		   
	    //printSpaces(depth);
	    //System.out.println(current.getName()); 
	    if("ul".equalsIgnoreCase(current.getName())){	
	    	List mainList = new ArrayList();
	    	
	    	List children = current.getChildren();
		    Iterator iterator = children.iterator();
		    while (iterator.hasNext()) {
		      Element child = (Element) iterator.next();
		      if("li".equalsIgnoreCase(child.getName())){
		    	  mainList.add(child);		    	  
		      }		      
		    }
	    	
		   
		    
		    Iterator it = mainList.iterator();
		    while (it.hasNext()) {
		    	Element child = (Element) it.next();
		    	List objlist = child.getChildren();
		    	for(int i=0;i< objlist.size();i++){
		    		Element obj = (Element) objlist.get(i);
		    		List paramlist = obj.getChildren();
		    		
		    		String lable=null,url=null;
		    		
		    		for(int j=0;j<paramlist.size();j++){
		    			//lable = null;url = null;
		    			Element param = (Element) paramlist.get(j);
		    			//
		    			if("Name".equalsIgnoreCase(param.getAttributeValue("name"))){
		    				//print("param:"+ param.getAttributeValue("name"));
		    				//print(param.getAttributeValue("value"));
		    				lable = param.getAttributeValue("value");
		    			}
		    			
		    				//print(param.getAttributeValue("value"));
		    			if("Local".equalsIgnoreCase(param.getAttributeValue("name"))){
		    				//print("param:"+ param.getAttributeValue("name"));
		    				//print(param.getAttributeValue("value"));
		    				url = param.getAttributeValue("value");
		    			}  				    				
		    				
		    		}
		    		if(lable!=null && url !=null);
		    		tocList.put(lable,url);	
		    		
		    	}
		    	//print(child.getName());
		    	
		    	
		    }
		     return tocList;
	    	
	    }
	    else{
	    	List children = current.getChildren();
		    Iterator iterator = children.iterator();
		    while (iterator.hasNext()) {
		      Element child = (Element) iterator.next();		     
		      tocList = getIndexMap(child, depth+1);
		    }
	    	
	    }
	    return tocList;    
	    
	  }
	  
	  	
	public void print(String st){
		System.out.println(st);
	}

	
	public void setOutputfile(String outputfile) {
		this.outputfile = outputfile;
	}

	

	public void setXmlfile(String xmlfile) {
		this.xmlfile = xmlfile;
	}
	
	public static void main(String[] args){
		System.out.println("hths");
		
	}
	//	 The setter for the "message" attribute
    public void setMessage(String msg) {
        this.msg = msg;
    }

}
